﻿using AutoMapper;
using Common.Property;
using Common.Utility;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;

namespace Api.Filter
{
    public class PermissionFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 验证登录
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            #region 允许匿名
            if (filterContext.ActionDescriptor.FilterDescriptors.Any(x => x.Filter is NoLoginAttribute))
            {
                return;
            }
            #endregion
            var result = new Result();
            var token = filterContext.HttpContext.Request.Headers["token"];
            if (string.IsNullOrWhiteSpace(token))
            {
                filterContext.Result = new JsonResult(Result.Failed(401, "无效Token"), new JsonSerializerSettings()
                {
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                });
                return;
            }
            RedisUtil redisUtil = new RedisUtil();
            var context = redisUtil.Get<LoginContext>($"LoginContext.{token}");
            if (context == null)
            {
                filterContext.Result = new JsonResult(Result.Failed(401, "无效Token"), new JsonSerializerSettings()
                {
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
                });
                return;
            }
            redisUtil.KeyExpire($"LoginContext.{token}", new TimeSpan(2, 0, 0, 0));

            //if (filterContext.ActionDescriptor.FilterDescriptors.Any(x => x.Filter is NoMemberAttribute))
            //{
            //    return;
            //}
            //if (string.IsNullOrWhiteSpace(context.MemberId))
            //{
            //    filterContext.Result = new JsonResult(Result.Failed(402, "未绑定用户"), new JsonSerializerSettings()
            //    {
            //        ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            //    });
            //    return;
            //}
            //var _memberContext = redisUtil.Get<MemberContext>($"MemberContext.{context.MemberId}");
            //if (_memberContext == null)
            //{
            //    var memberService = (ServiceLocator.Instance as ServiceProvider).GetService<IMemberService>();
            //    var member = memberService.GetById(context.MemberId);
            //    if (member == null)
            //    {
            //        context.MemberId = "";
            //        redisUtil.StringSet($"LoginContext.{token}", context, new TimeSpan(2, 0, 0, 0));
            //        filterContext.Result = new JsonResult(Result.Failed(402, "未绑定用户"), new JsonSerializerSettings()
            //        {
            //            ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
            //        });
            //        return;
            //    }

            //    var _mapper = (ServiceLocator.Instance as ServiceProvider).GetService<IMapper>();
            //    _memberContext = _mapper.Map<MemberContext>(member);
            //    redisUtil.StringSet($"MemberContext.{context.MemberId}", _memberContext, new TimeSpan(2, 0, 0, 0));
            //}
        }
    }


}